﻿using Autofac;

namespace Learn.Net8.Extensions
{
    public class AutofacPropertityModuleReg :Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var controllerBaseType = typeof(Microsoft.AspNetCore.Mvc.ControllerBase);
            builder.RegisterAssemblyTypes(typeof(Program).Assembly)
                .Where(t => controllerBaseType.IsAssignableFrom(t) && t!=controllerBaseType.BaseType)
                .PropertiesAutowired();

            //也可以使用下面的方式
            //builder.RegisterAssemblyTypes(typeof(Program).Assembly)
            //    .PropertiesAutowired();

            #region 自动生成
            //// 注册 Controller
            //builder.RegisterAssemblyTypes(ThisAssembly)
            //    .Where(t => t.Name.EndsWith("Controller"))
            //    .PropertiesAutowired()
            //    .InstancePerLifetimeScope();
            //// 注册 Service
            //builder.RegisterAssemblyTypes(ThisAssembly)
            //    .Where(t => t.Name.EndsWith("Service"))
            //    .PropertiesAutowired()
            //    .InstancePerLifetimeScope();
            //// 注册 Repository
            //builder.RegisterAssemblyTypes(ThisAssembly)
            //    .Where(t => t.Name.EndsWith("Repository"))
            //    .PropertiesAutowired()
            //    .InstancePerLifetimeScope();
            #endregion
        }
    }
}
